package com.lin.lostandfound.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lin.lostandfound.bean.ExpressThanks;
import com.lin.lostandfound.mapper.ExpressthanksMapper;
import com.lin.lostandfound.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.lin.lostandfound.annotation.SystemServiceLog;
import com.lin.lostandfound.service.IExpressThanksService;

@Service
public class ExpressThanksService implements IExpressThanksService{
	@Autowired
	private ExpressthanksMapper mapper;
	@Autowired
	private StringUtil util;
	/**
	 * 添加感谢信息
	 * @param expressThanks
	 * @return
	 */
	@SystemServiceLog(description = "添加感谢信息")
	public Boolean add(ExpressThanks expressThanks) {
		return mapper.insert(expressThanks)!=-1;
	}

	/**
	 * 批量修改状态
	 * @param thanksId
	 * @param status
	 * @return
	 */
	public Boolean modifyStatus(String thanksId, int status) {
		String[] idlist = thanksId.split(",");
		Long i = mapper.batchupdate(idlist, status);
		return i!=-1;
	}



	/**
	 * 修改状态
	 * @param thanksId
	 * @param status
	 * @return
	 */
	public Boolean modifyStatus(Long thanksId, int status) {
		ExpressThanks thanks = new ExpressThanks();
		thanks.setId(thanksId);
		thanks.setStatus(status);
		return mapper.updateById(thanks) != -1;
	}

	/**
	 * 批量删除感谢信息
	 * @param thanksId
	 * @return
	 */
	@SystemServiceLog(description = "批量删除感谢信息")
	public boolean remove(String thanksId) {
		String[] id = thanksId.split(",");
		
		int[] tId = new int[id.length];
		for(int i = 0 ;i < id.length; i++){
			tId[i] = Integer.parseInt(id[i]);
		}
		int i = mapper.deleteBatchIds(Arrays.asList(tId));
		return i != -1;
	}

	/**
	 * 查询所有统计数量
	 * @param key
	 * @return
	 */
	public Integer queryAllCount(String key) {
		QueryWrapper<ExpressThanks> wrapper = new QueryWrapper<>();
		if(util.notnull(key)){
			wrapper.like("title",key);
			wrapper.or();
			wrapper.like("userName",key);
		}
		int i = mapper.selectCount(wrapper);
		return i;
	}


	/**
	 * 根据条件分页查询
	 * @param key
	 * @param pageIndex
	 * @param pageSize
	 * @param sortField
	 * @param sortOrder
	 * @return
	 */
	public Map<String, Object> queryAll(String key, Integer pageIndex, Integer pageSize, String sortField, String sortOrder) {
		Page<ExpressThanks> page_ = new Page<>(pageIndex,pageSize);
		QueryWrapper<ExpressThanks> wrapper = new QueryWrapper<>();
		if(util.notnull(key)){
			wrapper.like("title",key);
			wrapper.or();
			wrapper.like("userName",key);
		}
		if (util.notnull(sortField)) {
			if(sortOrder.equals("desc")){
				wrapper.orderByDesc(sortField);
			}else{
				wrapper.orderByAsc(sortField);
			}
		}else{
			wrapper.orderByDesc("leaveTime");
		}
		IPage<ExpressThanks> page=mapper.selectPage(page_,wrapper);


		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", page.getRecords());
		map.put("total", page.getTotal());
		
		return map;
	}

	/**
	 * 根据id查询
	 * @param id
	 * @return
	 */
	public ExpressThanks queryById(Long id) {

		return mapper.selectById(id);
	}

}
